from typing import List, Optional
from dataclasses import dataclass, field


from dto.strategy_assessment import RiskAssessment
from service.das.stock_monitor_das import AntMonitorStock


@dataclass
class StrategyCalculateResult:
    # 股票信息
    stock_info: AntMonitorStock = None

    # 策略评估的结果列表
    risk_assessments: List[RiskAssessment] = field(default_factory=list)

    # 最新股价
    latest_price: Optional[float] = None

    # 计算的总得分
    total_score: int = 0

    # 风险等级（1-10）
    risk_level: int = 1

    # 关注等级（1-10）
    focus_level: int = 1

    def __post_init__(self):
        """
        初始化时计算风险等级和关注等级
        """
        if not self.risk_assessments:
            self.total_score = 0
            self.risk_level = 1
            self.focus_level = 1
        else:
            # 计算总得分
            self.total_score = sum(ra.node_point for ra in self.risk_assessments)

            for assessment in self.risk_assessments:
                # 判断策略分组
                if assessment.config.strategy_group == 0:  # 风险型策略
                    self.risk_level += (
                        assessment.node_point * assessment.config.strategy_level
                    )
                elif assessment.config.strategy_group == 1:  # 关注型策略
                    self.focus_level += (
                        assessment.node_point * assessment.config.strategy_level
                    )

    def __str__(self):
        return (
            f"Total Score: {self.total_score}, "
            f"Risk Level: {self.risk_level}, "
            f"Focus Level: {self.focus_level}, "
            f"Risk Assessments: {[ra.description for ra in self.risk_assessments]}"
        )
